{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "text='''\n",
    "<div>\n",
    "    <ul>\n",
    "        <li class='red'><h1>red flowers</h1></li>\n",
    "        <li class='yellow'><h2>yellow flowers</h2></li>\n",
    "        <li class='white'><h3>white flowers</h3></li>\n",
    "        <li class='black'><h4>black flowers</h4>\n",
    "    </ul>\n",
    "</div>\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "html=etree.HTML(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "html=etree.parse('file/lxml.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<lxml.etree._ElementTree at 0x108189e88>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "result=etree.tostring(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'<!DOCTYPE html>\\n<html>\\n<head>\\n\\t<title/>\\n</head>\\n<body>\\n\\t<div>\\n    <ul>\\n        <li class=\"red\"><h1>red flowers</h1></li>\\n        <li class=\"yellow\"><h2>yellow flowers</h2></li>\\n        <li class=\"white\"><h3>white flowers</h3></li>\\n        <li class=\"black\"><h4>black flowers</h4></li>\\n    </ul>\\n</div>\\n</body>\\n</html>'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_res(url):\n",
    "    headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}\n",
    "    response=requests.get(url,headers=headers)\n",
    "    return response.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n翻梵天之梦\\n']\n"
     ]
    }
   ],
   "source": [
    "url='https://www.qiushibaike.com/text/'\n",
    "selector=etree.HTML(get_res(url))\n",
    "ids=selector.xpath('//*[@id=\"qiushi_tag_121416685\"]/div[1]/a[2]/h2/text()')\n",
    "print(ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['\\n南九\\n']\n",
      "['\\n聊天不撩妹子\\n']\n",
      "['\\n胖香\\n']\n",
      "['\\n木子：李\\n']\n",
      "['\\n木子：李\\n']\n",
      "['\\n鱼歌浅唱\\n']\n",
      "['\\nAlone°祝英台…\\n']\n",
      "['\\n聊天不撩妹子\\n']\n",
      "['\\n好昵称都被抢空了\\n']\n",
      "['\\n孤狼独行！\\n']\n",
      "['\\n大灰机か\\n']\n",
      "['\\n南九\\n']\n",
      "['\\n余生陪你浪T^T\\n']\n",
      "['\\n鱼歌浅唱\\n']\n",
      "['\\n吃了两碗又盛\\n']\n",
      "['\\n吃了两碗又盛\\n']\n",
      "['\\n爱新觉罗.黄瓜\\n']\n",
      "['\\n挖鼻孔的老虎\\n']\n",
      "['\\n爱新觉罗.黄瓜\\n']\n"
     ]
    }
   ],
   "source": [
    "url='https://www.qiushibaike.com/text/'\n",
    "selector=etree.HTML(get_res(url))\n",
    "ids=selector.xpath('//div[@class=\"article block untagged mb15 typs_hot\"]')\n",
    "for item in ids:\n",
    "    id=item.xpath('div[1]/a[2]/h2/text()')\n",
    "    print(id)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
